package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentReferrerWalletOperateDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentReferrerWalletOperate;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentReferrerWalletOperateSearch;

public class AgentReferrerWalletOperateService {

    static Logger log = Logger.getLogger(AgentReferrerWalletOperateService.class);

    public static AgentReferrerWalletOperateService self(Date date) {
        return MultiDBTools.getService(AgentReferrerWalletOperateService.class, date);
    }

    public static AgentReferrerWalletOperateService self(String jdbcKey,Date date) {
        return MultiDBTools.getService(jdbcKey, AgentReferrerWalletOperateService.class, date);
    }

    AgentReferrerWalletOperateDao dao;

    public AgentReferrerWalletOperateService(String jdbcKey,Date date) {
        dao = new AgentReferrerWalletOperateDao(jdbcKey,date);
    }

    public List<AgentReferrerWalletOperate> findBySearch(AgentReferrerWalletOperateSearch search, Integer pageNo, Integer pageLimit){
        Conditions condition = dao.buildConditions(search);
        condition.desc("createDate");
        try {
            return dao.findByCondition(condition,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(AgentReferrerWalletOperateSearch search){
        Conditions condition = dao.buildConditions(search);
        try {
            return dao.count(condition);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public boolean addOperate(Long userId, Long referrerUserId,Long accountGroupId, Integer moneyType, Integer type, Long money, OperateInfo operateInfo) {
    	AgentReferrerWalletOperate operate = new AgentReferrerWalletOperate();
        operate.setUserId(userId);
        operate.setReferrerUserId(referrerUserId);
        operate.setAccountGroupId(accountGroupId);
        operate.setMoneyType(moneyType);
        operate.setType(type);
        operate.setMoney(money);
        operate.setOperatorType(operateInfo.getOperatorType());
        operate.setOperatorTypeName(operateInfo.getOperatorTypeName());
        operate.setOperator(operateInfo.getOperator());
        operate.setOperateNote(operateInfo.getOperateNote());
        operate.setCreateDate(new Date());
        try {
            return dao.insert(operate) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteByUserKeys(Long userId, Long referrerUserId){
        if(userId != null && referrerUserId != null){
            Conditions conditions = new Conditions(AgentReferrerWalletOperate.class);
            conditions.and().is("userId",userId);
            conditions.and().is("referrerUserId",referrerUserId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
